package cn.tedu.submarine;

import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.util.Random;

/**
 * 超类
 */
abstract public class SeaObject {

    public static final int LIVE = 0;//活着状态
    public static final int DEAD = 1;//死了状态
    public static final int REMOVE = 2;//删除状态
    public static final int PREPARE = 3;//准备状态
    private int state = LIVE;//默认活着状态

    private int width;
    private int hight;
    private int x;
    private int y;
    private int speed;
    private int life;


    //潜艇专用
    public SeaObject(int width,int hight){
        this.width = width;
        this.hight = hight;
        this.x = -width;
        Random rand = new Random();
        this.y = rand.nextInt(World.HEIGHT-hight-380+1)+380;
        this.speed = rand.nextInt(5)+1;
        this.life = World.levelNumber;
    }

    //战舰、boss
    public SeaObject(int width,int hight,int x,int y,int speed,int life){
        this.width = width;
        this.hight = hight;
        this.x = x;
        this.y = y;
        this.speed = speed;
        this.life = life;
    }
    //炸弹、水雷、boss炸弹
    public SeaObject(int width,int hight,int x,int y,int speed){
        this.width = width;
        this.hight = hight;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    //海洋对象移动
    abstract public void move();

    //获取图片
    abstract public ImageIcon getImage();

    public boolean isLive(){
        return state == LIVE;
    }

    public boolean isDead(){
        return state == DEAD;
    }

    public boolean isRemove(){
        return state == REMOVE;
    }

    public void paintImage(Graphics g){
        if(this.isLive() || this.isDead()){
            this.getImage().paintIcon(null,g,this.x,this.y);
        }
    }

    public boolean isOutBounds(){
        return this.x >= World.WIDTH;
    }


    public boolean isHit(SeaObject other){
        int x1 = this.x-other.width;
        int x2 = this.x+this.width;
        int y1 = this.y-other.hight;
        int y2 = this.y+this.hight;
        int x = other.x;
        int y = other.y;
        return x>=x1 && x<=x2
                &&
               y>=y1 && y<=y2;
    }

    public void goDead(){
        state = DEAD;
    }
    public void goRemove(){
        state = REMOVE;
    }

    public void subtractLife(){
        setLife(getLife()-1);
    }

    public void subtractLife(int fire){
        if (fire==1){
            setLife(getLife()-3);
        }else if (fire==2){
            setLife(getLife()-1);
        }

    }

    public void setLocation(){
        setX(getX()+getWidth()/3);
    }
    public int getWidth() {
        return width;
    }
    public void setWidth(int width) {
        this.width = width;
    }
    public int getHight() {
        return hight;
    }
    public void setHight(int hight) {
        this.hight = hight;
    }
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public int getSpeed() {
        return speed;
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }
    public int getLife() {
        return life;
    }
    public void setLife(int life) {
        this.life = life;
    }
    public int getState() {
        return state;
    }
    public void setState(int state) {
        this.state = state;
    }
}
